package software.literature.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DBDao {
	protected String dbPath;
	protected Connection connection;
	protected PreparedStatement preStm;
	protected ResultSet result;
	protected String DriverName = "org.sqlite.JDBC";
	protected String envPath = "jdbc:sqlite:";
	static {
		try {
			Class.forName("org.sqlite.JDBC");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public DBDao(String currentDB) {
		dbPath = currentDB;
	}

	protected void getConnection() {
		try {
			connection = DriverManager.getConnection(envPath + dbPath);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			System.out.println("get connection error");
			e.printStackTrace();
		}
	}

	protected void closeConnection() {

		try {
			if (null != connection) {
				connection.close();
			}
			if (null != preStm) {
				preStm.close();
			}
			if (null != result) {
				result.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
             e.printStackTrace();
		}
	}

}
